package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 添加购物车信息
     * @return
     */

    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){
        //
        QueryWrapper<ShoppingCart> wrapper =new QueryWrapper<>();
        wrapper.eq("user_id", BaseContext.get());
        wrapper.eq(shoppingCart.getDishId() != null, "dish_id", shoppingCart.getDishId());
        wrapper.eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId());

        //查询数据库有没有数据
        ShoppingCart cart = shoppingCartService.getOne(wrapper);
        if (cart==null){
            //把当前数据添加到数据库
            shoppingCart.setUserId(BaseContext.get());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        }else{
            //修改number的值
            cart.setNumber(cart.getNumber()+1);
            shoppingCartService.updateById(cart);
        }
        return R.success("添加成功");
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        QueryWrapper<ShoppingCart> wrapper =new QueryWrapper<>();
        wrapper.eq("user_id",BaseContext.get());
        List<ShoppingCart> cartList = shoppingCartService.list(wrapper);
        return R.success(cartList);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String>  clean(){
        QueryWrapper<ShoppingCart> wrapper =new QueryWrapper<>();
        wrapper.eq("user_id",BaseContext.get());
        shoppingCartService.remove(wrapper);
        return R.success("删除成功");
    }

    /**
     * 修改购物车里面的信息
     * @return
     */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        //创建条件对象
        QueryWrapper<ShoppingCart> wrapper =new QueryWrapper<>();
        wrapper.eq(shoppingCart.getDishId() != null, "dish_id", shoppingCart.getDishId());
        wrapper.eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId());
        //查询数据库里面的对象
        ShoppingCart cart = shoppingCartService.getOne(wrapper);
        //
        cart.setNumber(cart.getNumber()-1);
        //修改数据库里面的数据
        shoppingCartService.updateById(cart);
        if (cart.getNumber()==0){
            shoppingCartService.removeById(cart);
        }


        return R.success("修改成功");
    }
}
